﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlogSite.Models;

namespace BlogSite.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {
            //List of Blogs
            return View(Models.BlogHelper.Blogs.OrderBy(t => t.BlogId));
        }

        [HttpGet]
        public ActionResult Create()
        {
            Models.Blog Blog = new Models.Blog("", "", "");
            return View(Blog);
        }

        [HttpGet]
        public ActionResult Edit(int blogid)
        {
            Blog blog = BlogHelper.Blogs.Where(b => b.BlogId == blogid).FirstOrDefault();
            if (blog != null)
                return View(blog);

            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Edit(Blog blog)
        {
            Blog theblog = BlogHelper.Blogs.Where(b => b.BlogId == blog.BlogId).FirstOrDefault();
            if (theblog != null)
            {
                BlogHelper.Blogs.Remove(theblog);
                BlogHelper.Blogs.Add(blog);
            }

            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult Delete(int blogid)
        {
            Blog theblog = BlogHelper.Blogs.Where(b => b.BlogId == blogid).FirstOrDefault();
            if (theblog != null)
            {
                BlogHelper.Blogs.Remove(theblog);
            }

            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Create(int blogid, string name, string topic, string post)
        {
            Blog blog = new Blog { BlogId = blogid, Name = name, Topic = topic, Post = post };

            Models.BlogHelper.Blogs.Add(blog);

            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult Details(int blogid)
        {
            BlogCommentsViewModel vm = BlogHelper.GetBlogComments(blogid);
            if (vm != null)
            {
                return View(vm);
            }
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult DeleteComment(int commentid)
        {
            Comment comm = BlogHelper.Comments.Where(c => c.CommentId == commentid).FirstOrDefault();
            int bid = comm.BlogId;
            if (comm != null)
            {
                BlogHelper.Comments.Remove(comm);
            }
            return RedirectToAction("Details", new { blogid = bid });
        }

        [HttpGet]
        public ActionResult EditComment(int commentid, int blogid)
        {
            Comment comm = BlogHelper.Comments.Where(c => c.CommentId == commentid).FirstOrDefault();
            if (comm != null)
                return View(comm);

            return RedirectToAction("Details", new { blogid = blogid });
        }

        [HttpPost]
        public ActionResult EditComment(Comment comment)
        {
            Comment thecomment = BlogHelper.Comments.Where(c => c.CommentId == comment.CommentId).FirstOrDefault();
            if (thecomment != null)
            {
                BlogHelper.Comments.Remove(thecomment);
                BlogHelper.Comments.Add(comment);
            }

            return RedirectToAction("Details", new { blogid = comment.BlogId });
        }

        [HttpPost]
        public ActionResult CreateComment(Comment comment)
        {
            Models.BlogHelper.Comments.Add(comment);

            return RedirectToAction("Details", new { blogid = comment.BlogId });
        }
	}
}